﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CoreApi;
using System.Configuration;
using CoreService.Models;
using CoreApi.Meta;

namespace CoreService.Controllers
{
    public class UserWechatServiceController : BaseWechatController
    {
        private IUserService _userService = null;
        public UserWechatServiceController(IUserService service)
        {
            this._userService = service;
            _appKeys = ConfigurationManager.AppSettings["userKey"].Split(';');
        }

        [HttpPost]
        public IHttpActionResult CheckRegisterMobile([FromBody]RegisterMobileCheckRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = this._userService.CheckRegisterMobile(request.RegisterMobile);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult BindWechat([FromBody]BindWechatRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = this._userService.BindWechat(request.RegisterMobile, request.WechatUnionId);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult DeviceBrandList([FromBody]BaseRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = this._userService.DeviceBrandList();
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult DeviceModelList([FromBody]DeviceModelRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = this._userService.DeviceModelList(request.BrandId, request.DeviceType);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult AccountList([FromBody]WechatBaseRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.AccountList(request.WechatUnionId);
                return Ok<ReturnValue>(rv);
            }
            catch(Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult AccountAlarmList([FromBody]WechatBaseRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.AccountAlarmList(request.WechatUnionId);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult ServiceSheetList([FromBody]WechatBaseRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.ServiceSheetList(request.WechatUnionId);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }

        #region Situation

        [HttpPost]
        public IHttpActionResult BloodPressureSituation([FromBody]CitizenTimeRangeRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.BloodPressureSituation(request.WechatUnionId, request.CitizenId,request.TimeRange);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult GlycemicSituation([FromBody]CitizenTimeRangeRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.GlycemicSituation(request.WechatUnionId, request.CitizenId, request.TimeRange);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult SmokeAlarmSituation([FromBody]AccountTimeRangeRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.SmokeAlarmSituation(request.WechatUnionId, request.AccountId, request.TimeRange);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }

        #endregion

        #region Senior Citizen

        [HttpPost]
        public IHttpActionResult SenionCitizenList([FromBody]AccountRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.SenionCitizenList(request.WechatUnionId, request.AccountId);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult AddSeniorCitizen([FromBody]AddSeniorCitizenRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.AddSeniorCitizen(request.WechatUnionId, request.AccountId,request.Name,request.Gender,request.BirthdayYear,request.ContactMobile,request.IdCard,request.Height,request.Weight,request.NativePlace);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult UpdateSeniorCitizen([FromBody]UpdateSeniorCitizenRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.UpdateSeniorCitizen(request.WechatUnionId, request.CitizenId, request.Name, request.Gender, request.BirthdayYear, request.ContactMobile, request.IdCard, request.Height, request.Weight, request.NativePlace);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult RemoveSeniorCitizen([FromBody]CitizenRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.RemoveSeniorCitizen(request.WechatUnionId, request.CitizenId);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }

        #endregion

        #region Family Member

        [HttpPost]
        public IHttpActionResult FamilyMemberList([FromBody]AccountRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.FamilyMemberList(request.WechatUnionId, request.AccountId);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult AddFamilyMember([FromBody]AddFamilyMemberRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.AddFamilyMember(request.WechatUnionId, request.AccountId, request.RealName, request.RegMobile, request.OtherPhone, request.Province, request.City, request.District, request.Address, request.PostCode, request.CanManagement,request.LiveTogether);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult UpdateFamilyMember([FromBody]UpdateFamilyMemberRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.UpdateFamilyMember(request.WechatUnionId, request.MemberId, request.RealName, request.OtherPhone, request.Province, request.City, request.District, request.Address, request.PostCode, request.CanManagement,request.LiveTogether);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult RemoveFamilyMember([FromBody]MemberRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.RemoveFamilyMember(request.WechatUnionId, request.MemberId);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult ChangeFamilyMemberRegisterMobile([FromBody]ChangeMemberRegisterMobileRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.ChangeFamilyMemberRegisterMobile(request.WechatUnionId, request.MemberId, request.RegMobile);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }

        #endregion

        #region Device

        [HttpPost]
        public IHttpActionResult DeviceList([FromBody]AccountRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.DeviceList(request.WechatUnionId, request.AccountId);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult GetDeviceData([FromBody]DeviceRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.GetDeviceData(request.WechatUnionId, request.DeviceId);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult AddDevice([FromBody]AddDeviceRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.AddDevice(request.WechatUnionId, request.AccountId, request.BrandId, request.DeviceType, request.Model, request.DeviceSN,request.SIMSN);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult BindDeviceData([FromBody]BindDeviceDataRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.BindDeviceData(request.WechatUnionId, request.DeviceId,request.DataNumber, request.AccountId, request.CitizenId);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        [HttpPost]
        public IHttpActionResult RemoveDevice([FromBody]DeviceRequest request)
        {
            try
            {
                ReturnValue rv1 = CheckWechatRequest(request);
                if (!rv1.Passed) return Ok<ReturnValue>(rv1);
                ReturnValue rv = _userService.RemoveDevice(request.WechatUnionId, request.DeviceId);
                return Ok<ReturnValue>(rv);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }

        #endregion

    }
}
